package com.example.a828502.ormlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.a828502.ormlite.bean.TestBean;
import com.example.a828502.ormlite.bean.TestDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private Button btn, addbtn, deletebtn,updatebtn;
    private TestDao mTestDao;
    private List<TestBean> mTestBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.btn);
        addbtn = (Button)findViewById(R.id.addbtn);
        deletebtn = (Button)findViewById(R.id.deletebtn);
        updatebtn = (Button)findViewById(R.id.updatebtn);

        listView = (ListView)findViewById(R.id.listView);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryDatas();
            }
        });
        addbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addDatas();
            }


        });
        deletebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDatas();
            }
        });
        updatebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDatas();
            }
        });

        mTestDao = new TestDao(this);

    }




    private void queryDatas() {
//        mListSelectData = mSelectDataDao.queryProvinces();
        mTestBean = mTestDao.querySQLData();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        if (null!= mTestBean && mTestBean.size()> 0){
            for (TestBean data : mTestBean)
            {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("test_name", data.test_name);
                map.put("test_id", data.test_id);
                list.add(map);
            }

        }
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_2, new String[] { "test_name",
                "test_id" }, new int[] { android.R.id.text1,
                android.R.id.text2 });
        listView.setAdapter(adapter);
    }

    private void addDatas() {
        ArrayList<TestBean> datas = new ArrayList<TestBean>();
        TestBean data1 = new TestBean("00006fen","粉粉6");
        datas.add(data1);
        mTestDao.addTestBean(data1);
        queryDatas();

    }

    private void deleteDatas(){
        ArrayList<TestBean> datas = new ArrayList<TestBean>();
        TestBean data1 = new TestBean("00006fen","粉粉6");
        datas.add(data1);
        mTestDao.deleteTestBean(data1);
        queryDatas();

    }

    private void updateDatas(){
        ArrayList<TestBean> datas = new ArrayList<TestBean>();
        TestBean data1 = new TestBean("00006fen","粉粉7");
        datas.add(data1);
        mTestDao.updateTestBean(data1);
        queryDatas();

    }
}
